﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

namespace BattleCity.GUI
{
    public class HiScoreScreen : Screen
    {
        int expirationTime = 5;

        SpriteFont _font;
        Texture2D _back;
        Texture2D _tank1;
        Texture2D _tank2;
        Texture2D _tank3;
        Texture2D _tank4;
        Texture2D _arrow;
        Texture2D _line;

        SoundEffect _total;
        SoundEffect _hi;

        byte _LevelNumber;
        int _type1, _type2, _type3, _type4, _HiScore;
        double _time;
        bool _isNewRecord;

        List<byte> _enemyList;

        public event EventHandler TimeExpired;

        public HiScoreScreen(BattleCityGame game, SpriteFont font, List<Texture2D> txtList, List<SoundEffect> sList, byte number, int hiscore)
            : base(game)
        {
            _sprBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            _font = font;
            _arrow = txtList[0];
            _tank1 = txtList[1];
            _tank2 = txtList[2];
            _tank3 = txtList[3];
            _tank4 = txtList[4];
            _line = txtList[5];
            _back = txtList[6];
            _hi = sList[0];
            _total = sList[1];
            _HiScore = hiscore;
            _LevelNumber = number;
            game.newGame.GameOver += new EventHandler(PrepaireList);
            game.newGame.LevelCompleted += new EventHandler(PrepaireList);
        }
        void PrepaireList(object sender, EventArgs e)
        {
            _enemyList = (sender as GameScreen).DestroyedEnemiesList;

            if ((Game as BattleCityGame).newGame.player.Score > _HiScore)
                (Game as BattleCityGame).newGame.HiScore = _HiScore = (Game as BattleCityGame).newGame.player.Score;
            _type1 = _enemyList.Count(t => t == 1 || t == 5);
            _type2 = _enemyList.Count(t => t == 2 || t == 6);
            _type3 = _enemyList.Count(t => t == 3 || t == 7);
            _type4 = _enemyList.Count(t => t == 4 || t == 8);

            if (_isNewRecord)
            {
                _hi.Play();
                _isNewRecord = false;
            }
            else
                _total.Play();
        }
        public override void Update(GameTime gameTime)
        {
            _time += gameTime.ElapsedGameTime.TotalSeconds;
            if (_time >= expirationTime && TimeExpired != null)
                TimeExpired(this, new EventArgs());
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            _sprBatch.Draw(_back, new Rectangle(0, 0, 480, 416), null, Color.White, 0f, new Vector2(0f, 0f), SpriteEffects.None, 0.5f);
            _sprBatch.DrawString(_font, "HI-SCORE", new Vector2(128f, 32f), Color.Red, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _HiScore.ToString(), new Vector2(304f, 32f), Color.Orange, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "STAGE  " + _LevelNumber.ToString(), new Vector2(196f, 64f), Color.White);
            _sprBatch.DrawString(_font, "I-PLAYER", new Vector2(54f, 96f), Color.Red, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, (Game as BattleCityGame).newGame.player.Score.ToString(), new Vector2(118f, 128f), Color.Orange, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, ((int)_type1 * 100).ToString(), new Vector2(54f, 160f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "PTS", new Vector2(134f, 160f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _type1.ToString(), new Vector2(198f, 160f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_arrow, new Vector2(230f, 160f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_tank1, new Vector2(246f, 154f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, ((int)_type2 * 200).ToString(), new Vector2(54f, 208f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "PTS", new Vector2(134f, 208f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _type2.ToString(), new Vector2(198f, 208f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_arrow, new Vector2(230f, 208f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_tank2, new Vector2(246f, 202f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, ((int)_type3 * 300).ToString(), new Vector2(54f, 256f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "PTS", new Vector2(134f, 256f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _type3.ToString(), new Vector2(198f, 256f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_arrow, new Vector2(230f, 256f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_tank3, new Vector2(246f, 250f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, ((int)_type4 * 400).ToString(), new Vector2(54f, 304f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "PTS", new Vector2(134f, 304f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _type4.ToString(), new Vector2(198f, 304f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_arrow, new Vector2(230f, 304f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_tank4, new Vector2(246f, 298f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, "TOTAL", new Vector2(100f, 338f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.Draw(_line, new Vector2(196f, 332f), null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            _sprBatch.DrawString(_font, _enemyList.Count.ToString(), new Vector2(198f, 338f), Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.4f);
            base.Draw(gameTime);
        }
    }
}